package com.elitesland.org.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.org.param.OrgPostQParam;
import com.elitesland.org.service.OrgPostService;
import com.elitesland.org.vo.OrgPostVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/org/posts")
@Api(value = "组织结构", tags = {"岗位管理"})
public class OrgPostController {

    private OrgPostService orgPostService;

    @Autowired
    public void setOrgPostService(OrgPostService orgPostService) {
        this.orgPostService = orgPostService;
    }

    @PostMapping("/q")
    @ApiOperation("检索岗位信息")
    public ApiResult<?> search(@RequestBody OrgPostQParam param) {
        val ret = orgPostService.search(param);
        return ApiResult.ok(ret);
    }

    @GetMapping
    @ApiOperation("列出所有职位，按职位名排序")
    public ApiResult<?> listAll() {
        return ApiResult.ok(orgPostService.listAll());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据记录ID返回岗位信息")
    public ApiResult<?> oneById(@PathVariable Long id) {
        return orgPostService.oneById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @PostMapping
    @ApiOperation("创建岗位信息")
    public ApiResult<?> create(@RequestBody OrgPostVO post) {
        val id = orgPostService.create(post);
        return ApiResult.ok(id);
    }

    @PutMapping
    @ApiOperation("更新岗位信息")
    public ApiResult<?> update(@RequestBody OrgPostVO post) {
        orgPostService.update(post);
        return ApiResult.ok();
    }

    @DeleteMapping
    @ApiOperation("批量删除岗位信息")
    public ApiResult<?> removeByIds(@RequestBody List<Long> ids) {
        orgPostService.removeByIds(ids);
        return ApiResult.ok();
    }
}
